package cn.sdstudio.mapper;


import cn.sdstudio.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author zyq
 */
@Mapper
public interface UserMapper extends BaseMapperX<User> {

    @Select("SELECT * FROM tripvista.sys_user WHERE username = #{username} AND deleted = 0 LIMIT 1")
    User selectByUsername(@Param("username") String username);


    /**
     * 初始权限查询
     * @param userId
     * @return
     */
    @Select("""
        SELECT DISTINCT r.code
        FROM tripvista.sys_user u
        JOIN tripvista.sys_user_role ur ON u.id = ur.user_id
        JOIN tripvista.sys_role r ON ur.role_id = r.id
        JOIN tripvista.sys_role_permission rp ON r.id = rp.role_id
        JOIN tripvista.sys_permission p ON rp.permission_id = p.id
        WHERE u.id = #{userId}
          AND u.deleted = 0
          AND r.deleted = 0
          AND p.deleted = 0
          AND r.status = 1
          AND p.status = 1
    """)
    List<String> selectPermissionsByUserId(@Param("userId") Long userId);
}
